package fr.esipe.eioc3.timetableontime_v2;

import java.util.ArrayList;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ExpandableListView;

public class TimeTableDisplay extends Activity {

	private ExpandableListView expandableList = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.list_display);


		printTimeTable();

	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {

		Log.d("TimeTableDispay", "onTouchEvent => refreshing display");


		printTimeTable();
		
		return super.onTouchEvent(event);
	}
	
//=================== 	MY METHODS		===========================//
	
	/**
	 * Methode pour afficher l'emploi du temps
	 */
	public void printTimeTable(){
		
		int actualWeek = 0;
		int actualDay= 0;
		int i = 14;
		
		EventProvider tableGest = new EventProvider(this);
		Cursor element = tableGest.getEventsCursor();
		
		
		if(element.moveToFirst()){
		
			expandableList = (ExpandableListView) findViewById(R.id.listview);
	
			ArrayList<GroupDays> jours = new ArrayList<GroupDays>();
			actualWeek = element.getInt(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_WEEKNUMBER));
	
			while (actualWeek <= element.getInt(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_WEEKNUMBER))) 
			{
	
				String day = null;
				String month = null;
				Log.d("debug", "jours : " + i);
				
				GroupDays groupe;
				//Cas ou il n'y a pas du tout cours, parsedListevent = NULL
				if(element.getCount() == 1){
					
					groupe = new GroupDays("Pas cours (periode entreprise ou vacances");
				}
				else{
					if( element.getInt(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_DAYOFWEEK)) == 2)
						day = "Lundi";
					if( element.getInt(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_DAYOFWEEK)) == 3)
						day = "Mardi";
					if( element.getInt(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_DAYOFWEEK)) == 4)
						day = "Mercredi";
					if( element.getInt(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_DAYOFWEEK)) == 5)
						day = "Jeudi";
					if( element.getInt(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_DAYOFWEEK)) == 6)
						day = "Vendredi";
					if( element.getInt(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_DAYOFWEEK)) == 7)
						day = "Vendredi";
					
					if( element.getInt(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_MONTH)) == 0)
						month = "Janvier";
					if( element.getInt(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_MONTH)) == 1)
						month = "Fevrier";
					if( element.getInt(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_MONTH)) == 2)
						month = "Mars";
					if( element.getInt(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_MONTH)) == 3)
						month = "Avril";
					if( element.getInt(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_MONTH)) == 4)
						month = "Mai";
					if( element.getInt(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_MONTH)) == 5)
						month = "Juin";
					if( element.getInt(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_MONTH)) == 6)
						month = "Juillet";
					if( element.getInt(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_MONTH)) == 7)
						month = "Aout";
					if( element.getInt(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_MONTH)) == 8)
						month = "Septembre";
					if( element.getInt(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_MONTH)) == 9)
						month = "Octobre";
					if( element.getInt(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_MONTH)) == 10)
						month = "Novembre";
					if( element.getInt(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_MONTH)) == 11)
						month = "Decembre";
		
					groupe = new GroupDays(day + " " + element.getInt(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_DAYOFMONTH)) + " " + month);
	
				}
				Log.d("debug", day + " " + element.getInt(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_DAYOFMONTH)) + " " + element.getString(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_MONTH)));
	
				ArrayList<ChildLabels> donnees = new ArrayList<ChildLabels>();
	
				actualDay = element.getInt(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_DAYOFMONTH));
	
	
				while(actualDay == element.getInt(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_DAYOFMONTH))){
					
					donnees.add(new ChildLabels(groupe, element.getString(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_LABEL0)), 
							element.getInt(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_STARTHOUR)), 
							element.getInt(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_STARTMINUTE)),
							element.getInt(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_ENDHOUR)), 
							element.getInt(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_ENDMINUTE)),
							element.getString(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_LABEL1)),
							element.getString(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_LABEL2)),
							element.getString(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_LABEL3)),
							element.getString(element.getColumnIndex(EventOpenHelper.EVENT_COLUMN_LABEL4))));
	
					if (element.isLast() == true)
						break;
					
					element.moveToNext();
				}
	
				Log.d("coucou", "jours");
				groupe.setObjets(donnees);
				jours.add(groupe);
				if (element.isLast() == true)
					break;
				i++;
			}
	
			TimeTableListView adapter = new TimeTableListView(this, jours);
			expandableList.setAdapter(adapter);
		}else{
			Log.d("TimeTableDispay", "cursor to display il null");

		}
		
		tableGest.close();
		element.close();		
	}

}

